# -*- coding: utf-8 -*-
# @Time    : 2025/6/15 14:56
# @Author  : XinHuang
# @Email   : SA24231052@mail.ustc.edu.cn
# @File    : log
# @Software: PyCharm
import logging


def setup_logging(log_file='log.log'):
    """
    设置日志配置
    """
    logging.basicConfig(
        level=logging.DEBUG,
        format="%(pathname)s:%(lineno)d\n%(asctime)s [%(levelname)s] %(message)s",
        datefmt="%Y-%m-%d %H:%M:%S",
        encoding='utf-8',
        handlers=[
            logging.FileHandler(log_file),  # 将日志写入文件
            logging.StreamHandler()  # 同时输出到控制台
        ]
    )
    print('')  # 让控制台输出更好看
    logging.debug('已经设置好日志配置')

def progress_bar_iteration(iteration, total, prefix='', suffix='', decimals=1, length=50, fill='█', print_end="\r"):
    """
    打印进度条的函数，根据迭代显示进度调
    :param iteration: 当前迭代次数
    :param total: 总迭代次数
    :param prefix: 进度条前缀字符串
    :param suffix: 进度条后缀字符串
    :param decimals: 显示的小数位数
    :param length: 进度条的长度（字符数）
    :param fill: 进度条填充字符
    :param print_end: 每次打印结束后的字符，默认为回车符，用于在同一行更新进度条
    """
    percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
    filled_length = int(length * float(percent) / 100)
    bar = fill * filled_length + '-' * (length - filled_length)
    print(f'\r{prefix} |{bar}| {percent}% {suffix}', end=print_end)
    # 当进度完成时，打印换行符
    if iteration == total:
        print()


def progress_bar_target(now, target, prefix='', suffix='', decimals=1, length=50, fill='█', print_end="\r"):
    """
    打印进度条的函数，根据现在的值和目标值显示进度调
    :param now: 当前值
    :param target: 目标值
    :param prefix: 进度条前缀字符串
    :param suffix: 进度条后缀字符串
    :param decimals: 显示的小数位数
    :param length: 进度条的长度（字符数）
    :param fill: 进度条填充字符
    :param print_end: 每次打印结束后的字符，默认为回车符，用于在同一行更新进度条
    """
    # 当进度完成时，打印100%
    if now <= target:
        bar = fill * length
        print(f'\r{prefix} |{bar}| 100.0% {suffix}\n', end=print_end)
    else:
        percent = ("{0:." + str(decimals) + "f}").format(100 * (1.0 / (now - target + 1)))
        filled_length = int(length * float(percent) / 100)
        bar = fill * filled_length + '-' * (length - filled_length)
        print(f'\r{prefix} |{bar}| {percent}% {suffix}', end=print_end)
